package com.unknowpupil.design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: <p>
 * 主题对象
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/4/28 16:26
 * @Classname: Subject
 * @Version: 1.0
 */
public class Subject {
    /**
     * 所有观察者
     */
    protected List<Observer> list = new ArrayList<>();

    public void registerObserver(Observer obs){
        list.add(obs);
    }

    public void removeObserver(Observer obs){
        list.remove(obs);
    }

    /**
     * 通知所有观察者更改状态
     */
    public void notifyAllObservers(){
        for(Observer objs : list){
            objs.update(this);
        }
    }

}
